<?php

	class User extends Model{
	
		private $_UserId;
		private $_Username;
		private $_Password;
		private $_Name;
		private $_Surname;
		private $_Active;

		function __construct($arrInitialValues = null){
			parent::__construct();

			if (isset($arrInitialValues['user_id']))
				$this->setUserId($arrInitialValues['user_id']);
			else
				$this->setUserId(0);

			if (isset($arrInitialValues['username']))
				$this->setUsername($arrInitialValues['username']);
			else
				$this->setUsername('');

			if (isset($arrInitialValues['password']))
				$this->setPassword($arrInitialValues['password']);
			else
				$this->setPassword('');

			if (isset($arrInitialValues['name']))
				$this->setName($arrInitialValues['name']);
			else
				$this->setName('');

			if (isset($arrInitialValues['surname']))
				$this->setSurname($arrInitialValues['surname']);
			else
				$this->setSurname('');

			if (isset($arrInitialValues['active']))
				$this->setActive($arrInitialValues['active']);
			else
				$this->setActive(0);
		}
		
		/* GETTERS AND SETTERS */
		public function getUserId(){
			return $this->_UserId;
		}
		
		public function setUserId($value){
			$this->_UserId = $value;
		}
		
		public function getUsername(){
			return $this->_Username;
		}
		
		public function setUsername($value){
			$this->_Username = $value;
		}
		
		public function getPassword(){
			return $this->_Password;
		}
		
		public function setPassword($value){
			$this->_Password = $value;
		}
		
		public function getName(){
			return $this->_Name;
		}
		
		public function setName($value){
			$this->_Name = $value;
		}
		
		public function getSurname(){
			return $this->_Surname;
		}
		
		public function setSurname($value){
			$this->_Surname = $value;
		}

		public function getActive(){
			return $this->_Active;
		}
		
		public function setActive($value){
			$this->_Active = $value;
		}

		public function isActive(){
			return $this->getActive();
		}

		public static function getAll(){
			parent::requireDB();

			$query = '
				SELECT *
				FROM users
				ORDER BY username ASC';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}

		public function getActiveUser(){
			$query = '
				SELECT *
				FROM users
				WHERE username = "'. $this->getUsername() . '"
					AND password = "' . $this->getPassword() . '"
					AND active = 1';

			$dbHelper = new DBHelper($query);
			$user = $dbHelper->getResult();
			if (empty($user))
				return array();

			return $user[0];
		}

		public function getUser(){
			$query = '
				SELECT *
				FROM users
				WHERE user_id = ' . $this->getUserId();
			
			$dbHelper = new DBHelper($query);
			return $dbHelper->getResult();
		}

		private function changeStatus(){
			$query='
				UPDATE users
				SET active = ' . $this->getActive() . '
				WHERE user_id= ' . $this->getUserId();
			$dbHelper = new DBHelper($query);
			return ($dbHelper->getNumRows() > 0);
		}
		
		public function activate(){
			$this->setActive(1);
			return $this->changeStatus();
		}
		
		public function delete(){
			$this->setActive(0);
			return $this->changeStatus();
		}
		
		public function updatePassword(){
			$query = '
				UPDATE users
				SET password = "' . $this->getPassword() . '" 
				WHERE user_id = ' . $this->getUserId();

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
		
		public function update(){
			$query = '
				UPDATE users
				SET name = "' . $this->getName() . '",
					surname = "' . $this->getSurname() . '",
					username = "' . $this->getUsername() . '",
					active = ' . $this->isActive() . ' 
				WHERE user_id = ' . $this->getUserId();

			$dbHelper = new DBHelper($query);
			return 1;
		}
		
		public function create(){
			$query = '
				INSERT INTO users
				(username, password, name, surname, active)
				VALUES
				("' . $this->getUsername() . '", "' . $this->getPassword() . '", "' . $this->getName() . '", "' . $this->getSurname() . '", ' . $this->getActive() . ')';

			$dbHelper = new DBHelper($query);
			return $dbHelper->getNumRows();
		}
	}

?>